﻿#if NETFX
using Color = System.Windows.Media.Color;
using Colors = System.Windows.Media.Colors;
#elif NETFX_CORE || WINDOWS_PHONE
using Color = Windows.UI.Color;

#if NETFX_CORE
using Colors = Windows.UI.Colors;
#endif

#endif



using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace NicoView.Model.Service
{
    /// <summary>
    /// 通知可能な視聴ユーザ情報
    /// </summary>
    public sealed class NotificationNicoUser:NicoViewObservableObject
    {
        /// <summary>
        /// 内部保有のユーザ情報
        /// </summary>
        internal SerializableNicoEncounterUser User;


        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="core"></param>
        public NotificationNicoUser(SerializableNicoEncounterUser core)
        {
            User = core;
            _nickName = "";
            _userIconPath = "";
        }


        /// <summary>
        /// ニコニコに設定されているニックネーム
        /// </summary>
        private string _nickName;

        private string _userIconPath;

        /// <summary>
        /// ニックネームプロパティ
        /// </summary>
        public string NickName
        {
            get
            {
                return this._nickName;
            }

            set
            {
                if (this._nickName == value) return;
                this._nickName = value;
                RaisePropertyChanged("NickName");
            }
        }

        /// <summary>
        /// 匿名ユーザか判定
        /// </summary>
        public bool IsAnonymous
        {
            get
            {
                return this.User.IsAnonymous;
            }
        }


        /// <summary>
        /// コテハンが設定されているか判定
        /// </summary>
        public bool HasBeenSetCustomName
        {
            get
            {
                return ! string.IsNullOrEmpty(this.CustomName);
            }
        }

        /// <summary>
        /// ユーザサムネイル画像パス
        /// </summary>
        public string UserIconPath
        {
            get
            {
                return this._userIconPath;
            }
            set
            {
                if (this._userIconPath == value) return;
                this._userIconPath = value;
                RaisePropertyChanged("UserIconPath");
            }
        }


        /// <summary>
        /// ニコニコAPIを使用して取得できる情報が設定されているか判定
        /// </summary>
        /// <returns></returns>
        public bool IsSetApiInfo()
        {
            return   this.NickName != "" && this.UserIconPath != "" ;
        }

        /// <summary>
        /// ニコニコAPIに情報が取得可能なユーザか判定
        /// </summary>
        public bool SetRequest
        {
            get
            {
                return (!this.IsAnonymous) && (!IsSetApiInfo()) && long.Parse(this.Id) < 500000000 && this._userIconPath == "";
            }
        }


        /// <summary>
        /// コテハン
        /// </summary>
        public string CustomName
        {
            get
            {
                return this.User.CustomName;
            }
            set
            {
                if (this.User.CustomName == value) return;
                this.User.CustomName = value;
                RaisePropertyChanged("CustomName");
            }
        }

        /// <summary>
        /// ユーザID
        /// </summary>
        public string Id
        {
            get
            {
                return this.User.Id;
            }
            set
            {
                if (this.User.Id == value) return;
                this.User.Id = value;
                RaisePropertyChanged("Id");
            }

        }

        private static ICollection<NamedColor> backGroundColors;


        /// <summary>
        /// 背景色候補
        /// </summary>
        public static ICollection<NamedColor> BackGroundColors
        {
            get
            {
                if (backGroundColors == null)
                {

#if NETFX || NETFX_CORE
                    var colorProps = typeof(Colors).GetRuntimeProperties();
#elif WINDOWS_PHONE
                    var colorProps = typeof(System.Windows.Media.Colors).GetRuntimeProperties();
#endif
                    var list = new List<NamedColor>();
                    list.Add(NamedColor.NoneColor);
                    foreach (var colorProp in colorProps)
                    {
#if NETFX || NETFX_CORE
                        var color = (Color)colorProp.GetValue(null, null);
                        
#elif WINDOWS_PHONE
                        var systemColor = (System.Windows.Media.Color)colorProp.GetValue(null, null);
                        var color = Color.FromArgb(systemColor.A, systemColor.R, systemColor.G, systemColor.B);

#endif
                        if(checkBackColor(color))
                        {
                            list.Add(new NamedColor(color, colorProp.Name));
                        }
                        
                    }
                    backGroundColors = list;
                }

                return backGroundColors;
            }
        }

        /// <summary>
        /// 背景色にするべきか判定
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private static bool checkBackColor(Color color)
        {
            return (color.B > 25  && color.R > 25  && color.G > 25) && (!(color.B >230) && !(color.R > 230) && !(color.G > 230))  ;
        }



        /// <summary>
        /// 背景用の色をランダムで取得
        /// </summary>
        /// <returns></returns>
        public static NamedColor GetRundomColor()
        {
            var random = new Random();
            NamedColor selectColor = null;

            do
            {
                var index = random.Next(1, BackGroundColors.Count - 1);
                selectColor = BackGroundColors.ElementAt(index);

            } while (selectColor == beforeRundumBackGroundColor);

            return selectColor;
        }

        /// <summary>
        /// 直前に選択した背景色
        /// </summary>
        private static NamedColor beforeRundumBackGroundColor = null;

        /// <summary>
        /// 設定されている背景色
        /// </summary>
        public NamedColor BackGroundColor
        {
            get
            {
                if (this.User.BackGroundColor == null) return NamedColor.NoneColor;
                return this.User.BackGroundColor;
            }

            set
            {
                if (this.User.BackGroundColor == value) return;
                if (value == NamedColor.NoneColor)
                {
                    this.User.BackGroundColor = null;
                }
                else
                {
                    this.User.BackGroundColor = value;
                }
                
                RaisePropertyChanged("BackGroundColor");
            }
        }

    }
}
